package com.niit.galgame.service.impl;

import com.niit.dao.CollectDao;
import com.niit.dao.GameDao;
import com.niit.dao.UpvoteDao;
import com.niit.dao.VedioDao;
import com.niit.entity.Game;
import com.niit.entity.Vedio;
import com.niit.galgame.service.GameService;
import com.niit.utils.HdfsTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service
public class GameServiceImpl implements GameService {
    @Resource
    private GameDao gameDao;
    @Resource
    private HdfsTool hdfsTool;
    @Resource
    private VedioDao vedioDao;
    @Resource
    private UpvoteDao upvoteDao;
    @Resource
    private CollectDao collectDao;

    @Override
    public int insertGame(Game game) {
        gameDao.insert(game);
        return game.getGid();
    }

    @Override
    public void upLoadFile(InputStream inputStream, String fileName, int gid) {
        hdfsTool.putFileToHdfs(inputStream,fileName,gid);
    }

    @Override
    public InputStream getFileStream(String fileName, int gid) throws Exception {
        return hdfsTool.getFromHdfs(fileName,gid);
    }

    @Override
    public void deleteGame(int gid) throws Exception {
        hdfsTool.deleteDir(gid);
        gameDao.deleteById(gid);
        vedioDao.deleteByGid(gid);
        upvoteDao.deleteByGid(gid);
        collectDao.deleteByGid(gid);
    }

    @Override
    public int insertVedio(Vedio vedio) {
        return vedioDao.insert(vedio);
    }

    @Override
    public Game queryById(int gid) {
        return gameDao.queryById(gid);
    }

    @Override
    public List<Game> findAll() {
        return gameDao.queryAll();
    }


}
